/***************************************************************************
 * This code and information is provided "as is" without warranty of any   *
 * kind, either expressed or implied, including but not limited to the     *
 * implied warranties of merchantability and/or fitness for a particular   *
 * purpose.                                                                *
 *                                                                         *
 * Copyright (C) 2008 Teridian SemiConductor, Corp. All Rights Reserved.   *
 *                                                                         *
 ***************************************************************************/
//**************************************************************************//
//                                                                          // 
//  DESCRIPTION: TSCP-CCID                                                  //
//               PacketParser.cs                                            //
//  PacketParser is an object that interprets the message received from     //
//  serial port. The input for this object is a message string              //
//                                                                          //
//  AUTHOR:  SD                                                             //
//                                                                          //
//  HISTORY: 2007 SEPTEMBER 26; First Version Released 1.0                  //
//           2007 DECEMBER  14; Version 2.00 Release                        //
//                                                                          //
//**************************************************************************//
/***************************************************************************
 * This code is an original creation. It is intended to be used for a one  *
 * reader, one card situation within the Micro Framework. This is intended *
 * to be used to generate a new device class to support a reader chip not  *
 * already supported with this API.                                        *
 * LAST MODIFIED: 2010 April 23                                            *
 * Copyright (C) 2009 Gemalto                                              *
 ***************************************************************************/
/***************************************************************************
 *  PacketParser.cs is part of the PC/SC Micro API for the .NET Micro      *
 *  Framework.                                                             *
 *                                                                         *
 *  The PC/SC Micro API for the .NET Micro Framework is free software:     *
 *  you can redistribute it and/or modify it under the terms of the GNU    *
 *  General Public License as published by the Free Software Foundation,   *
 *  either version 2 of the License, or (at your option) any later version.*
 *                                                                         *
 *  The PC/SC Micro API for the .NET Micro Framework is distributed in     *
 *  the hope that it will be useful, but WITHOUT ANY WARRANTY; without even*
 *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    *
 *  PURPOSE.  See the GNU General Public License for more details.         *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with the PC/SC Micro API for the .NET Micro Framework. If not,   *
 *  see <http://www.gnu.org/licenses/>.                                    *
 ***************************************************************************/
using System;
using Microsoft.SPOT;

namespace PCSCMicro
{
    class PacketParser
    {        
        // Definition of acknowledge bytes
        // Change strings to values used by reader
        const string ACK = "ACK";
        const string NACK = "NACK";

        // Parsed data from response
        internal string Message;
        internal string ResMsg;
        internal string ErrorMessage;
        internal string statusWord;
        internal string resp;
        internal int respLen;
        internal ErrorType ErrorCode;
        
        // Potential error types
        internal enum ErrorType
        {
            None, NoResponse, Timeout, NACK, IllegalMessage
            // Include other card specific error codes
        }

        // Only one constructor to discourage bad uFW coding practices
        // (inherited code had another constructor with msg as parameter)
        internal PacketParser()
        {
            Message = "";
        }

        // Encouraged to call this every time new message is received
        // instead of creating new PacketParser each time
        internal void GetPacket(string msg)
        {
            Message = msg;
            ParseMe();
        }
        
        void ParseMe()
        {
            // No response
            if ((Message == "") || (Message == null))
            {
                ResMsg = "";
                ErrorMessage = "No response";
                ErrorCode = ErrorType.NoResponse;
            }
            // ACK received, response falls into infinite loop
            else if (Message == ACK)
            {
                ResMsg = ACK;
                ErrorMessage = "Timeout expired & no data received." + "\r\n" + "Increasing timeout may help. ";
                ErrorCode = ErrorType.Timeout;
            }
            //NACK received
            else if (Message == NACK)
            {
                ResMsg = NACK;
                ErrorMessage = "NACK limit reached";
                ErrorCode = ErrorType.NACK;
            }
            // Illegal received message
            // Minimum length of a legal message is ...

            else if (Message.Length < 8)
            {
                ResMsg = Message;
                ErrorMessage = "Illegal message";
                ErrorCode = ErrorType.IllegalMessage;
            }

        }
    }
}
